package org.vt.scheduleapp;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;

public class EventReader {
	private final String FILENAME = "event_file.txt";
	private FileInputStream fis;
	//private final String newLine = "[\n]";
	private final String comma = "[,]";
	private final String s_colon = "[;]";
	private Context mCtx;
	
	public EventReader(Context context)
	{
		mCtx = context;
	}
	
	public List<CalendarEvent> getAllEvents() throws IOException, FileNotFoundException
	{
		fis = mCtx.openFileInput(FILENAME);
		List<CalendarEvent> events = new ArrayList<CalendarEvent>();
		BufferedReader br = new BufferedReader(new InputStreamReader(fis));
		String line = br.readLine();
		String eventName = "";
		String sDate = "";
		String eDate = "";
		String sTime = "";
		String eTime = "";
		String repeat = "";
		String location = "";
		//Toast.makeText(mCtx, line, Toast.LENGTH_SHORT).show();
		while((line = br.readLine())!=null)
		{
			String[] t1 = line.split(comma);
			
			if(t1.length<7)
			{return events;}
			String[] n = t1[0].split(s_colon);
			if(n.length<2)
				{return events;}
			else
				{eventName = n[1];}

			
			String[] a = t1[1].split(s_colon);
			if(a.length<2)
			{return events;}
			else{sDate = a[1];}
			
			String[] b = t1[2].split(s_colon);
			if(b.length<2)
			{return events;}
			else{eDate = b[1];}
			
			String[] c = t1[3].split(s_colon);
			if(c.length<2)
			{return events;}
			else{sTime = c[1];}
			
			String[] d = t1[4].split(s_colon);
			if(d.length<2)
			{return events;}
			else{eTime = d[1];}
			
			String[] e = t1[5].split(s_colon);
			if(e.length<2)
			{return events;}
			else{repeat = e[1];}
			
			String[] f = t1[6].split(s_colon);
			if(e.length<2)
			{return events;}
			else{location = f[1];}
			
			events.add(new CalendarEvent(eventName, sDate, eDate, sTime, eTime, repeat, location));
		}
		fis.close();
		return events;
	}

}
